package com.lottery.material.controller.portal.report;

import com.lottery.material.aspect.CheckLogIn;
import com.lottery.material.common.JsonResult;
import com.lottery.material.controller.base.BaseController;
import com.lottery.material.entity.report.ReportRecord;
import com.lottery.material.exception.ResponseException;
import com.lottery.material.service.portal.report.ReportRecordService;
import com.lottery.material.utils.CalendarUtil;
import com.lottery.material.utils.ExceptionUtil;
import com.lottery.material.utils.RedisUtil;
import com.lottery.material.vo.report.ReportVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * Description:    举报接口（用户、文章、评论）
 * Author:         sunny
 * CreateDate:     2019-04-11 09:34
 */
@RestController
@RequestMapping("/portal")
@Slf4j
public class ReportController extends BaseController {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private ReportRecordService reportRecordService;

    /**
     * description: 举报（用户、文章、评论）
     * author sunny
     * createDate 2019-04-11 上午 10:41
     * @param reportVo
     * @return
     */
    @PostMapping("/report")
    @CheckLogIn
    public JsonResult getReportTypeList(@Valid ReportVo reportVo) {
        try {
            //参数验证
            if (StringUtils.isBlank(reportVo.getUserId())) {
                log.info("用户唯一标识不能为空！");
                return JsonResult.failure(400, "用户唯一标识不能为空！");
            }
            if (StringUtils.isBlank(reportVo.getAccessToken())) {
                log.info("访问令牌不能为空！");
                return JsonResult.failure(400, "访问令牌不能为空！");
            }
            if (StringUtils.isNotBlank(reportVo.getReportScope()) &&
                    ("1".equals(reportVo.getReportScope()) || "2".equals(reportVo.getReportScope()))) {
                if (StringUtils.isBlank(reportVo.getReportCode())) {
                    log.info("举报编号不能为空！");
                    return JsonResult.failure(400, "举报编号不能为空！");
                }else if(StringUtils.isNotBlank(reportVo.getReportCode()) &&
                        ("RYF00001".equals(reportVo.getReportCode()) || "RWZ00001".equals(reportVo.getReportCode()))){
                    if(StringUtils.isBlank(reportVo.getReportContent())){
                        log.info("举报内容不能为空！");
                        return JsonResult.failure(400, "举报内容不能为空！");
                    }
                }
            }

            //封装数据进行保存
            ReportRecord reportRecord = new ReportRecord();
            reportRecord.setCreateTime(CalendarUtil.getSystemCurrentDate());
            reportRecord.setReportScope(Integer.valueOf(reportVo.getReportScope()));
            reportRecord.setReportCode(reportVo.getReportCode());
            reportRecord.setReportContent(reportVo.getReportContent());
            reportRecord.setReportUserId(reportVo.getUserId());
            reportRecord.setBeReportObjectId(reportVo.getBeReportObjectId());
            reportRecord.setResultCode(1);  //设置待审核
            boolean result = reportRecordService.saveReportRecord(reportRecord);

            if(result){
                return JsonResult.success("举报成功");
            }else{
                return JsonResult.failure(400, "举报失败");
            }
        } catch (Exception e) {
            log.error("获取举报类型列表出现异常", ExceptionUtil.logExceptionStack(e));
            throw new ResponseException(500, "网络异常！");
        }
    }
}
